<template>
  <!--列表 -->
  <div class="flex-1 flex relative overflow-hidden">
    <BaseList :data-source="dataSource" :loading="loading" class="flex-1 overflow-auto pb-10">
      <template #renderItem="{ item }">
        <div class="py-2 px-3 flex justify-between items-center list-items" :class="{ active: activeLabel?.id === item.id }" @click="onSelect(item)">
          <div class="overflow-hidden">
            <div class="name flex items-center">
              <div class="w-24 font-semibold truncate" :title="item.labelName">
                {{ item.labelName }}
              </div>
              <div class="ml-2 flex">
                <a-tag v-if="item.status" color="#f31260">停用</a-tag>
                <a-tag v-if="!item.status" color="#12a150">启用</a-tag>
                <a-tag v-if="item.inputType === 0" color="#3758f1">单选</a-tag>
                <a-tag v-else color="#d46b08">多选</a-tag>
              </div>
            </div>
            <div class="mt-1 truncate text-xs text-gray-400">{{ item.memo || '-' }}</div>
          </div>
          <div class="dropdown">
            <a-dropdown :get-popup-container="(trigger) => trigger.parentNode">
              <div class="px-2 py-1 hover:bg-gray-300 rounded flex items-center"><EllipsisOutlined :style="{ fontSize: '18px' }" /></div>
              <template #overlay>
                <a-menu>
                  <a-menu-item key="2">
                    <BaseButton v-if="item.status" size="small" type="link" @click="onClickStatus(item, 0)">启用</BaseButton>
                    <BaseButton v-else confirm-text="确认停用？" danger size="small" type="link" @click="onClickStatus(item, 1)">停用</BaseButton>
                  </a-menu-item>
                </a-menu>
              </template>
            </a-dropdown>
          </div>
        </div>
      </template>
    </BaseList>
  </div>
</template>
<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import { EllipsisOutlined } from '@ant-design/icons-vue'
import { post } from '@/utils/request'

const props = defineProps({
  rowData: {
    type: Object,
    default: () => ({})
  }
})
const emit = defineEmits(['select'])

const activeLabel = ref({})
const params = ref({ pxfs: 2 })
const { loading, dataSource, getData } = useFetch({
  url: '/operation/opex/getDictLabelList',
  params,
  immediate: true,
  onSuccess: (data) => {
    emit('select', data?.[0] || {})
    activeLabel.value = data?.[0] || {}
    // store.currentRole = data?.[0] || {}
  }
})
watch(
  () => [props.rowData?.status],
  () => getData(),
  { immediate: true }
)
const onClickStatus = async (row, status) => {
  const { code, msg } = await post('/operation/opex/doUpdateDictLabelStatus', { ...row, status })
  if (code !== 0) return message.error(msg)
  message.success(msg)
  getData()
}
const onSelect = (select) => {
  activeLabel.value = select
  emit('select', select)
}
defineExpose({
  getData
})
</script>
<style lang="less" scoped>
.list-items {
  cursor: pointer;
  &:hover {
    .dropdown {
      visibility: visible;
    }
  }
  .name {
    .ant-tag {
      padding: 0 4px;
      font-weight: normal;
      font-size: 12px;
    }
  }

  .dropdown {
    visibility: hidden;
    transition: all 0.3s;
  }
}
</style>
